//
//  MCPersonInformationTableViewCell.m
//  TLYL
//
//  Created by MC on 2017/6/14.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCPersonInformationTableViewCell.h"
#import "MCMineInfoModel.h"

#define HEIGHT_LINE  55

@interface MCPersonInformationTableViewCell ()
/*
 * 打底
 */
@property (nonatomic,strong)UIView * backView;



@end

@implementation MCPersonInformationTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    self.backgroundColor=[UIColor clearColor];
    
    
    /*
     * 打底
     */
    _backView=[[UIView alloc]init];
    [self addSubview:_backView];
    _backView.backgroundColor=[UIColor whiteColor];
    _backView.frame=CGRectMake(0, 0, G_SCREENWIDTH, 55*5);
    
    /*
     * 用户名
     */
    
    _userNameTextField = [[UITextField alloc] init];
    [self setTextField:_userNameTextField WithPlaceholder:@"加载中" and:UIKeyboardTypeNumberPad andIndex:0 andTitle:@"用户名"];
    /*
     * 返点
     */
    _rebateTextField = [[UITextField alloc] init];
    [self setTextField:_rebateTextField WithPlaceholder:@"加载中" and:UIKeyboardTypeNumberPad andIndex:1 andTitle:@"返点"];
    
    /*
     * 真实姓名
     */
    ;
    _realNameTextField = [[UITextField alloc] init];
    [self setTextField:_realNameTextField WithPlaceholder:@"加载中" and:UIKeyboardTypeNumberPad andIndex:2 andTitle:@"真实姓名"];

    
    
    /*
     * 手机号
     */
    _phoneTextField = [[UITextField alloc] init];
    [self setTextField:_phoneTextField WithPlaceholder:@"11位手机号码" and:UIKeyboardTypeNumberPad andIndex:3 andTitle:@"手机号"];

    
    
//    
//    _tipLab =[[UILabel alloc]initWithFrame:CGRectZero];
//    _tipLab.textColor=RGB(69, 142, 226);
//    _tipLab.font=[UIFont systemFontOfSize:12];
//    _tipLab.text =@"(选填)";
//    _tipLab.textAlignment=NSTextAlignmentRight;
//    [_backView addSubview:_tipLab];
//    [_tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.equalTo(_backView.mas_top);
//        make.centerY.equalTo(_phoneTextField.mas_centerY);
//        make.width.mas_equalTo(50);
//        make.right.equalTo(_backView.mas_right).offset(-10);
//        
//    }];
//    
    
    
    
    
    /*
     * 邮箱
     */
    _emailTextField=[[UITextField alloc] init];
    _emailTextField.tag=1002;
    [self setTextField:_emailTextField WithPlaceholder:@"请输入你的常用邮箱" and:UIKeyboardTypeEmailAddress andIndex:4 andTitle:@"邮箱"];


}

-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type andIndex:(int)index andTitle:(NSString *)title{
    
    UILabel * lab=[[UILabel alloc]init];
    lab.text=title;
    lab.textColor=RGB(68, 68, 68);
    [_backView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backView.mas_top).offset(HEIGHT_LINE*index);
        make.left.equalTo(_backView.mas_left).offset(10);
        make.height.mas_equalTo(HEIGHT_LINE);
        make.width.mas_equalTo(80);
    }];
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(54, 128, 211);
    textField.textAlignment = NSTextAlignmentLeft;
    textField.returnKeyType = UIReturnKeyDone;
    textField.keyboardType = type;
    [textField setValue:RGB(177, 177, 177) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];

    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    [_backView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backView.mas_top).offset(HEIGHT_LINE*index);
        make.left.equalTo(lab.mas_right).offset(0);
        make.right.equalTo(_backView.mas_right).offset(-10);
        make.height.mas_equalTo(HEIGHT_LINE);
    }];
    if (index<4) {
        UIView * lineView=[[UIView alloc]init];
        [_backView addSubview:lineView];
        lineView.backgroundColor=RGB(200, 200, 200);
        lineView.frame=CGRectMake(10, HEIGHT_LINE*(index+1), G_SCREENWIDTH-20, 0.5);
 
    }
    
    
}


-(void)relayOutConstraints{
   
    
}

-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
    MCMineInfoModel *mineInfoModel = [MCMineInfoModel sharedMCMineInfoModel];

    _userNameTextField.text=[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    _userNameTextField.userInteractionEnabled=NO;

    _rebateTextField.text=mineInfoModel.MyRebate;
    _rebateTextField.userInteractionEnabled=NO;
    
    _realNameTextField.text=mineInfoModel.UserRealName;
    
    if (mineInfoModel.UserRealName.length>0) {
        _realNameTextField.text=[NSString stringWithFormat:@"%@**",[mineInfoModel.UserRealName substringToIndex:1]];
        _realNameTextField.userInteractionEnabled=NO;
    }

    if (mineInfoModel.Mobile.length>0) {
        if (mineInfoModel.Mobile.length>5) {
            _phoneTextField.text=[NSString stringWithFormat:@"%@****%@",[mineInfoModel.Mobile substringToIndex:3],[mineInfoModel.Mobile substringFromIndex:(mineInfoModel.Mobile.length-2)]];
        }else{
            _phoneTextField.text=mineInfoModel.Mobile;
        }
        _phoneTextField.userInteractionEnabled=NO;
    }
    if (mineInfoModel.EMail.length>0) {
        
        if([mineInfoModel.EMail rangeOfString:@"@"].location !=NSNotFound){
            NSArray * arr=[mineInfoModel.EMail componentsSeparatedByString:@"@"];
            if ([arr[0] length]>3) {
                _emailTextField.text=[NSString stringWithFormat:@"%@****@%@",[arr[0] substringToIndex:3],arr[1]];
            }else if([arr[0] length]==3){
                _emailTextField.text=[NSString stringWithFormat:@"%@****@%@",[arr[0] substringToIndex:2],arr[1]];
            }else if([arr[0] length]==2){
                _emailTextField.text=[NSString stringWithFormat:@"%@****@%@",[arr[0] substringToIndex:1],arr[1]];
            }else if([arr[0] length]==1){
                _emailTextField.text=[NSString stringWithFormat:@"****@%@",arr[1]];
            }
        }else{
            _emailTextField.text=mineInfoModel.EMail;
        }
        _emailTextField.userInteractionEnabled=NO;
    }
   
}

-(void)textFieldDidChange:(UITextField *)textfield{
    //手机号
    if (textfield.tag==1001) {
         //11位电话号码
        if (_phoneTextField.text.length>10) {
            _phoneTextField.text =  [_phoneTextField.text substringToIndex:11];
        }
    //邮箱
    }else if (textfield.tag==1002){
    
        
    //姓名
    }else if (textfield.tag==1003){
        
    }
}

+(CGFloat)computeHeight:(id)info{
    
    return 55*5;
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
